package org.marsdonne.sync;

import com.fasterxml.jackson.databind.JsonNode;
import com.vladmihalcea.hibernate.type.json.JsonBinaryType;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.TypeDef;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.util.Date;

@Data
@Entity
@EntityListeners(AuditingEntityListener.class)
@NoArgsConstructor
@TypeDef(typeClass = JsonBinaryType.class, defaultForType = JsonNode.class)
public class Record {
    @Id
    @GeneratedValue(generator = "snowflake")
    @GenericGenerator(name = "snowflake", strategy = "org.marsdonne.commons.SnowFlake")
    private Long id;
    private Long version;//Event#version
    private Action action;
    private String domain;
    @Column(columnDefinition = "jsonb")
    private JsonNode content;
    private Date dateSynced;
    @CreatedDate
    private Date dateCreated;

    public Record(long version, Action action, String domain, JsonNode content, Date dateSynced) {
        this.action = action;
        this.domain = domain;
        this.content = content;
        this.version = version;
        this.dateSynced = dateSynced;
    }

    public Record(Action action, String domain, JsonNode content, Date dateSynced) {
        this(System.currentTimeMillis(), action, domain, content, dateSynced);
    }
}
